/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minValue;

/**
 *
 * @author mj
 */
class Linex {

    private Point first;
    private Point second;

    public Linex(Point p1, Point p2) {

        first = p1;
        second = p2;
    }

    void comparelx(Linex lx2) throws ZeroException, IntersectException, TripeException {
        int p0 = first.compare(lx2.getfirst());
        int p1 = second.compare(lx2.getsecond());
        if ((p0 == 0 && p1 != 0) || (p1 == 0 && p0 != 0)) {
            throw (new TripeException());
        }
        if (p0 == 0 && p1 == 0) {
            throw (new ZeroException());
        }
        if (p0 != p1) {
            throw (new IntersectException());
        }
    }

    private Point getfirst() {
        return first;
    }

    private Point getsecond() {
        return second;
    }

    double trapezium(Linex lx2) {
        double h1 = Math.abs(first.getY() - lx2.getfirst().getY());
        double h2 = Math.abs(second.getY() - lx2.getsecond().getY());

        return (h1 + h2) * 1.3 / 2;
    }

    double interaction(Linex lx2) {


        Point xp = computerinsection(lx2);
        Triplex a1 = new Triplex(getfirst(), lx2.getfirst(), xp);
        Triplex a2 = new Triplex(xp, getsecond(), lx2.getsecond());

        return a1.aer() + a2.aer();
    }

    private Point computerinsection(Linex lx2) {
        double x = (lx2.getB() - getB()) / (getK() - lx2.getK());
        double y = getK() * x + getB();
        return new Point(x, y);
    }

    private double getK() {
        return (second.getY() - first.getY()) / (second.getX() - first.getX());
    }

    private double getB() {
        return first.getY() - getK() * first.getX();
    }

    double Triple(Linex lx2) {
        int p0 = first.compare(lx2.getfirst());
        int p1 = second.compare(lx2.getsecond());
        if (p0 == 0) {
            Triplex tx = new Triplex(first, second, lx2.getsecond());
            return tx.aer();

        }
        if (p1 == 0) {
            Triplex tx = new Triplex(first, lx2.getfirst(), lx2.getsecond());
            return tx.aer();
        }
        return 0;
    }

    boolean compareTwoLine(Linex lx2) {
      double  l1=  getsecond().getY() -  getfirst().getY();
      double  l2 = lx2.getsecond().getY() - lx2.getfirst().getY();
  
      if(symbolcompute(l1) ==  symbolcompute(l2))
      {
          return  true;
      }
        return   false;
    }

    private int symbolcompute(double l1) {
        if(l1 > 0  )
        {
            return  1;
        }else if(l1 == 0 )
        {
            return   0;
        }
          return   -1;
    }
}
